#include "../include/head.h"

// 创建双向循环链表
PUserNode user_creat_list() {
    PUserNode head = (PUserNode)malloc(sizeof(UserNode));
    if(head == NULL) {
        printf("内存分配失败\n");
        return NULL;
    }
    head->front = head->next = head;
    return head;
}

// 尾插法插入用户节点
int user_tail_insert(PUserNode head, users value) {
    if(head == NULL) {
        printf("链表为空\n");
        return -1;
    }

    PUserNode newnode = (PUserNode)malloc(sizeof(UserNode));
    if(newnode == NULL) {
        printf("内存分配失败\n");
        return -1;
    }

    memcpy(&newnode->info, &value, sizeof(users));

    // 插入到链表尾部
    newnode->front = head->front;
    newnode->next = head;
    head->front->next = newnode;
    head->front = newnode;

    return 0;
}

// 用户登录验证
PUserNode user_login(PUserNode head) {
    printf("-------------------------------用户登录页面---------------------------\n");
    char name[20], pwd[20];
    printf("请输入用户名: ");
    scanf("%19s", name);
    getchar();
    printf("请输入密码: ");
    scanf("%19s", pwd);
    getchar();
    
    PUserNode p = head->next;
    while(p != head) {
        if(strcmp(p->info.username, name) == 0) {
            if(strcmp(p->info.password, pwd) == 0) {
                printf("欢迎 %s 登录系统\n", name);
                printf("您的权限是: %s\n", p->info.user_type == USER_TYPE_ADMIN ? "管理员" : "普通用户");
                return p;
            }else{
                printf("密码错误\n");
                return NULL;
            }
        }
        p = p->next;
    }

    printf("用户不存在\n");
    
    return NULL;
}

// 注册用户
void users_register(PUserNode head) {
    printf("-------------------------------用户注册页面---------------------------\n");
    users user;
    printf("请输入用户名: ");
    scanf("%19s", user.username);
    printf("请输入密码: ");
    scanf("%19s", user.password);
    printf("请输入用户类型(0-普通用户,1-管理员): ");
    scanf("%d", &user.user_type);
    getchar();

    // 检查用户名是否已存在
    PUserNode p = head->next;
    while(p != head) {
        if(strcmp(p->info.username, user.username) == 0) {
            printf("用户名已存在\n");
            return;
        }
        p = p->next;
    }

    if(user_tail_insert(head, user) == 0) {
        user_write_file(head);
        printf("用户添加成功\n");
    } else {
        printf("用户添加失败\n");
    }
    
}

// 修改用户名
int modify_username(PUserNode currentUser, PUserNode userList) {
    char newname[20];
    printf("请输入新用户名: ");
    scanf("%19s", newname);
    getchar();
    
    // 检查新用户名是否已存在
    PUserNode p = userList->next;
    while(p != userList) {
        if(strcmp(p->info.username, newname) == 0 && p != currentUser) {
            printf("错误: 用户名 '%s' 已存在\n", newname);
            return 0;
        }
        p = p->next;
    }
    
    // 更新用户名
    strcpy(currentUser->info.username, newname);
    user_write_file(userList);
    printf("用户名修改成功!\n");
    return 1;
}

// 修改用户密码
int modify_user_password(PUserNode currentUser, PUserNode userList) {
    char currentPwd[20], newPwd[20], confirmPwd[20];
    
    printf("请输入当前密码: ");
    scanf("%19s", currentPwd);
    getchar();
    
    // 验证当前密码
    if(strcmp(currentUser->info.password, currentPwd) != 0) {
        printf("错误: 当前密码不正确\n");
        return 0;
    }
    
    printf("请输入新密码: ");
    scanf("%19s", newPwd);
    getchar();
    
    printf("请确认新密码: ");
    scanf("%19s", confirmPwd);
    getchar();
    
    // 验证新密码
    if(strcmp(newPwd, confirmPwd) != 0) {
        printf("错误: 两次输入的密码不一致\n");
        return 0;
    }
    
    if(strcmp(currentPwd, newPwd) == 0) {
        printf("错误: 新密码不能与当前密码相同\n");
        return 0;
    }
    
    // 更新密码
    strcpy(currentUser->info.password, newPwd);
    user_write_file(userList);
    printf("密码修改成功!\n");
    return 1;
}